package fc.sj.study.utils;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.security.MessageDigest;

/**
 * Created by Administrator on 2017/3/16.
 */

public class OkHttpUtil {
    String baseurl="http://api.qmgy.gs/index.php/Api/Index/getHomeNotice";
    public void test(StringCallback callback){
        OkHttpUtils.post()
                .url(baseurl)
                .build()
                .execute(callback);
    }

    public static void GetToken(StringCallback callback){
        String a=String.valueOf(System.currentTimeMillis() / 1000);
        String b=sha1("pOtQkjX1fjM" + "123456" + a);
        OkHttpUtils.post()
                .url("http://api.cn.ronghub.com/user/getToken.json")
                .addHeader("App-Key","lmxuhwaglz1md")
                .addHeader("Nonce","123456")
                .addHeader("Timestamp",a)
                .addHeader("Signature",b)
                .addHeader("Content-Type","application/x-www-form-urlencoded")
                .addParams("userId","1000")
                .addParams("name","fz")
                .addParams("portraitUri","")
                .build()
                .execute(callback);
        LogUtil.e("TAG",a+","+b);
    }

    //SHA1加密//http://www.rongcloud.cn/docs/server.html#通用_API_接口签名规则
    private static String sha1(String data){
        StringBuffer buf = new StringBuffer();
        try{
            MessageDigest md = MessageDigest.getInstance("SHA1");
            md.update(data.getBytes());
            byte[] bits = md.digest();
            for(int i = 0 ; i < bits.length;i++){
                int a = bits[i];
                if(a<0) a+=256;
                if(a<16) buf.append("0");
                buf.append(Integer.toHexString(a));
            }
        }catch(Exception e){

        }
        return buf.toString();
    }
}
